﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NLog;

namespace Se.Log
{
    public class SeLogger
    {
        public SeLogger(SeLogLevel minLevel = SeLogLevel.Debug, SeLogLevel maxLevel = SeLogLevel.Fatal, bool isConsoleShow = true)
        {
            this._minLevel = minLevel;
            this._maxLevel = maxLevel;
            this._isConsoleShow = isConsoleShow;
        }

        private Logger _logger = LogManager.GetCurrentClassLogger();
        private Logger _sqlLogger = LogManager.GetLogger("Sql");
        private Logger _requestLogger = LogManager.GetLogger("Request");
        private Logger _responseLogger = LogManager.GetLogger("Response");
        private Logger _loginLogger = LogManager.GetLogger("Login");
        private Logger _registerLogger = LogManager.GetLogger("Register");
        private SeLogLevel _minLevel;
        private SeLogLevel _maxLevel;
        private bool _isConsoleShow;

        public void Debug(string message, params object[] objs)
        {
            if (_minLevel <= SeLogLevel.Debug && SeLogLevel.Debug <= _maxLevel)
            {
                if (this._isConsoleShow)
                {
                    Console.ForegroundColor = ConsoleColor.Blue;
                    Console.WriteLine(message, objs);
                }
                _logger.Debug(message, objs);
            }
        }

        public void Info(string message, params object[] objs)
        {
            if (_minLevel <= SeLogLevel.Info && SeLogLevel.Info <= _maxLevel)
            {
                if (this._isConsoleShow)
                {
                    Console.ForegroundColor = ConsoleColor.White;
                    Console.WriteLine(message, objs);
                    _logger.Info(message, objs);
                }
            }
        }

        public void Warn(string message, params object[] objs)
        {
            if (_minLevel <= SeLogLevel.Warn && SeLogLevel.Warn <= _maxLevel)
            {
                if (this._isConsoleShow)
                {
                    Console.ForegroundColor = ConsoleColor.Cyan;
                    Console.WriteLine(message, objs);
                    _logger.Warn(message, objs);
                }
            }
        }

        public void Error(string message, params object[] objs)
        {
            if (_minLevel <= SeLogLevel.Error && SeLogLevel.Error <= _maxLevel)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine(message, objs);
                _logger.Error(message, objs);
            }
        }

        public void Fatal(string message, params object[] objs)
        {
            if (_minLevel <= SeLogLevel.Fatal && SeLogLevel.Fatal <= _maxLevel)
            {
                Console.ForegroundColor = ConsoleColor.Magenta;
                Console.WriteLine(message, objs);
                _logger.Fatal(message, objs);
            }
        }

        public void Sql(string message, params object[] objs)
        {
            Info(message, objs);
            _sqlLogger.Info(message, objs);
        }

        public void Request(string message, params object[] objs)
        {
            Info(message, objs);
            _requestLogger.Info(message, objs);
        }

        public void Response(string message, params object[] objs)
        {
            Info(message, objs);
            _responseLogger.Info(message, objs);
        }

        public void Login(string message, params object[] objs)
        {
            Info(message, objs);
            _loginLogger.Info(message, objs);
        }

        public void Register(string message, params object[] objs)
        {
            Info(message, objs);
            _registerLogger.Info(message, objs);
        }

        ~SeLogger()
        {
            NLog.LogManager.Flush();
            Console.ResetColor();
        }
    }

    /// <summary>
    /// 日志类型
    /// </summary>
    public enum SeLogLevel : byte
    {
        None = 0,
        Debug = 1,
        Info = 2,
        Warn = 3,
        Error = 4,
        Fatal = 5,
    }

}
